package com.campus.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.common.ActivityStatusConstant;
import com.campus.common.core.Pager;
import com.campus.common.core.Result;
import com.campus.common.core.TableData;
import com.campus.model.dto.PageResult;
import com.campus.model.TManagerActivity;
import com.campus.model.dto.ManagerActivityConditionDto;
import com.campus.model.dto.ManagerActivityCreateDto;
import com.campus.model.dto.ManagerActivityUpdateDto;
import com.campus.service.IManagerActivityService;
import com.campus.web.vo.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/activity")
@Api(tags = "活动管理")
public class ManagerActivityController {

    @Autowired
    private IManagerActivityService managerActivityService;

    @PostMapping("/add")
    @ApiOperation("添加活动")
    public Result<Integer> createManagerActivity(@RequestBody @Validated ManagerActivityCreateDto managerActivityCreateDto){
        return Result.success(managerActivityService.createActivity(managerActivityCreateDto));
    }

    @PostMapping("update")
    @ApiOperation("修改活动")
    public Result<Integer> updateManagerActivity(@RequestBody @Validated ManagerActivityUpdateDto managerActivityUpdateDto){
        return Result.success(managerActivityService.updateManagerActivity(managerActivityUpdateDto));
    }

    @PostMapping("publish/{id}")
    @ApiOperation("发布活动")
    public Result<Integer> publishActivity(@PathVariable Long id){
        TManagerActivity activityById = managerActivityService.getActivityById(id);
        if (activityById == null) {
            return Result.error("活动不存在");
        }else if (activityById.getStatus() == ActivityStatusConstant.PUBLISHED) {
            return Result.error("活动已发布");
        }else if (activityById.getStatus() == ActivityStatusConstant.FINISHED) {
            return Result.error("活动已结束");
        }
        return Result.success(managerActivityService.publishActivity(id));
    }

    @GetMapping("list")
    @ApiOperation("活动列表")
    public TableData<TManagerActivity>selectActivitiesByConditions(ManagerActivityConditionDto managerActivityConditionDto, Pager pager){

        Page<TManagerActivity> p = managerActivityService.selectActivitiesByConditions(managerActivityConditionDto,pager);

        return TableData.success(p.getRecords(), p.getTotal());
    }



}
